###############################################################
# Proximity detection
#
# A script designed to run on a router running DD-WRT to detect certain devices connected to the router.
# It runs at startup and runs continually, checking for a specific list of devices (phones/laptop, etc)
# that connect wirelessly to the router.  Once a device is connected, the status is updated on the ISY
# with either a 0=disconnected, or a 1=connected.  State variables are used on the ISY so that a change 
# in status (0/1) can trigger a programe on the ISY. 
#
# The searching frequency can be adjusted to be slower/faster depending on your requirements. Searching too fast
# could burden your router.  Too slow might not update the status as necessary for the application.
#



# Make changes below
# MAC address of each device to watch. Don't leave blank. 
# For security purposes, if your router requires a password, even if someone could clone the MAC of your 
# phone, they would still require the password of your network to link to your router. 
macdevice1="aa:aa:aa:aa:aa:aa"    #FOB1
macdevice2="bb:bb:bb:bb:bb:bb"    #FOB2
macdevice3="cc:cc:cc:cc:cc:cc"    #FOB3
macdevice4="dd:dd:dd:dd:dd:dd"    #FOB4

FOB1ip="xxx.xxx.xxx.xxx"
FOB2ip="xxx.xxx.xxx.xxx"
FOB3ip="xxx.xxx.xxx.xxx"
FOB4ip="xxx.xxx.xxx.xxx"

#ISY username, password, and IP Address
username="user"
password="password"
ISYip="xxx.xxx.xxx.xxx"

# State variable numbers on the ISY corresponding to each MAC above
# isyvartot is the cummulative number of devices (of the 4 above) that are
# currently connected.
isyvar1=1
isyvar2=2
isyvar3=3
isyvar4=4
isyvartot=5

# Occupied and unoccupied delay in seconds to check status
# Adjust for shorter/longer wait times.  For instance, when one device is already 
# connected, you might want to check less frequently.  This could also delay the 
# notification of a disconnect.
delay_occupied=4
delay_unoccupied=2

# initial testing loop count - uncomment the counter near the bottom of the script for testing only. 
limit=120

###############################################
# do not change below here
###############################################

sleep 
#initialize internal variables

# status of each MAC. 0=disconnected. 1=connected.  -1 initially forces isy update first loop
macconnected1=-1
macconnected2=-1
macconnected3=-1
macconnected4=-1
connected=-1
# total number of currently conencted devices.   
currentconnected=0
counter=1

# Initial testing loop.  Will run continually after testing is complete
while [ $counter -lt $limit ]; do


#reset current status. Two variables are used for each device.  The past known status and the current 
# status.  Only a change is reported to the ISY.  Otherwise, it would constantly be updating the ISY with 
# the current status creating unnecessary traffic for both the router and the ISY
maccurrent1=0;
maccurrent2=0;
maccurrent3=0;
maccurrent4=0;

# compare each device that is currently connected to the MAC devices we want to watch.
# changed the following to chek for each MAC

maccurrent1=$(arping -c 4 -I br0 $FOB1ip | grep -c $macdevice1)
if [ $maccurrent1 -gt 0 ]; then
   maccurrent1=1
fi

maccurrent2=$(arping -c 4 -I br0 $FOB2ip | grep -c $macdevice2)
if [ $maccurrent2 -gt 0 ]; then
   maccurrent2=1
fi

maccurrent3=$(arping -c 4 -I br0 $FOB3ip | grep -c $macdevice3)
if [ $maccurrent3 -gt 0 ]; then
   maccurrent3=1
fi

maccurrent4=$(arping -c 4 -I br0 $FOB4ip | grep -c $macdevice4)
if [ $maccurrent4 -gt 0 ]; then
   maccurrent4=1
fi

# Look for a change in status from the old known to the current status.
# If it changed, update the ISY. Otherwise it leaves it as is. 
if [ $macconnected1 -ne $maccurrent1 ]; then 
     wget -q http://$username:$password@$ISYip/rest/ ... accurrent1
fi

if [ $macconnected2 -ne $maccurrent2 ]; then
     wget -q http://$username:$password@$ISYip/rest/ ... accurrent2
fi

if [ $macconnected3 -ne $maccurrent3 ]; then
     wget -q http://$username:$password@$ISYip/rest/ ... accurrent3
fi   
  
if [ $macconnected4 -ne $maccurrent4 ]; then
     wget -q http://$username:$password@$ISYip/rest/ ... accurrent4
fi

# Update the known status from the current.  Ready for the next loop. 
macconnected1=$maccurrent1;
macconnected2=$maccurrent2;
macconnected3=$maccurrent3;
macconnected4=$maccurrent4;

# Total up the number of devices connected. 
let currentconnected=$macconnected1+$macconnected2+$macconnected3+$macconnected4

# Look for a change, and update the ISY. 
if [ $connected -ne $currentconnected ]; then
   wget -q http://$username:$password@$ISYip/rest/ ... tconnected
fi

connected=$currentconnected

# Delay (sleep) depending on the connection status. 
# No devices connected could delay less.  Once a device is connected, it could delay longer. 
if [ $connected -gt 0 ]; then
   sleep $delay_occupied
   else
   sleep $delay_occupied
fi

#for testing only - uncomment to have the looping stop at X loops defined in variable:  limit. 
#let counter=$counter+1   
done